﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuickORM.BLL;
using QuickORM.Common;
using Wuqi.Webdiyer;

public partial class Manage_WebPages_DevManage_Manager_ManagerList : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Query(string.Empty);
        }
    }

    protected void Query(string sqlstring)
    {
        var templist = BLLFactory<T_ManagerInfoManager>.Instance.Find(sqlstring);
        AspNetPager1.RecordCount = templist.Count;
        rptList.DataSource = templist.Take(AspNetPager1.PageSize);
        rptList.DataBind();
    }

    protected void btn_query_Click(object sender, EventArgs e)
    {
        var sqlstring = string.Empty;
        if (!string.IsNullOrEmpty(dpManagerType.SelectedValue))
        {
            sqlstring += "M_type = '" + Util.querySQL(dpManagerType.SelectedValue) + "' and";
        }

        sqlstring = sqlstring.Length == 0 ? string.Empty : sqlstring.Remove(sqlstring.Length - 3);
        Query(sqlstring);
        ViewState["query"] = sqlstring;
    }

    //protected void btn_edit_Click(object sender, EventArgs e)
    //{
    //    int m_id = 0;
    //    for (int i = 0; i < rptList.Items.Count; i++)
    //    {
    //        CheckBox cb = (CheckBox)rptList.Items[i].FindControl("cbkItem");
    //        if (cb.Checked)
    //        {
    //            HiddenField hf = (HiddenField)rptList.Items[i].FindControl("hfID");
    //            m_id = Int32.Parse(hf.Value);
    //        }
    //    }
    //    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "openDialogResize('ManagerListAdd.aspx?action=edit&id=" + m_id + "', '800', '600');", true);
    //}

    protected void btn_delete_Click(object sender, EventArgs e)
    {
        var js = "alert('操作失败！');";
        var tempdelid = new List<int>();
        for (var i = 0; i < rptList.Items.Count; i++)
        {
            var cb = (CheckBox) rptList.Items[i].FindControl("cbkItem");
            if (cb.Checked)
            {
                var hf = (HiddenField) rptList.Items[i].FindControl("hfID");
                var personid = int.Parse(hf.Value);
                tempdelid.Add(personid);
            }
        }
        var sqlstring = "M_Id in (";
        foreach (var item in tempdelid)
        {
            sqlstring += "'" + item + "',";
        }
        sqlstring = sqlstring.Remove(sqlstring.Length - 1) + ")";

        using (var temptran = BLLFactory<T_ManagerInfoManager>.Instance.CreateTransaction())
        {
            try
            {
                BLLFactory<T_ManagerInfoManager>.Instance.DeleteByCondition(sqlstring, temptran);
                temptran.Commit();
                js = "alert('删除成功！');";
            }
            catch (Exception)
            {
                temptran.Rollback();
                js = "alert('删除失败！');";
            }
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "", js, true);
            Query(ViewState["query"] != null ? ViewState["query"].ToString() : "");
        }
    }

    protected void linkBtnDown_Click(object sender, EventArgs e)
    {
        //LinkButton linkBtnDel = sender as LinkButton;
        //if (linkBtnDel.CommandName == "del")
        //{
        //    int id = Convert.ToInt32(linkBtnDel.CommandArgument);
        //    BLLFactory<T_UserInfoManager>.Instance.Delete(id);
        //    Query(ViewState["name"] != null ? ViewState["name"].ToString() : "");
        //}
    }

    #region 分页处理

    protected void AspNetPager1_PageChanging(object src, PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        var templist = BLLFactory<T_ManagerInfoManager>.Instance.Find(ViewState["query"] != null ? ViewState["query"].ToString() : "");
        rptList.DataSource = templist.Skip(AspNetPager1.PageSize*(e.NewPageIndex - 1)).Take(AspNetPager1.PageSize);
        rptList.DataBind();
    }

    #endregion 分页处理

    protected void btn_add_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "openDialogResize('ManagerListAdd.aspx', '800', '600');", true);
    }
}